package fhedu;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;

import obj.Lichhoc;

public class KXmlCalendarParser
{
	public Vector parse(String rssUrl) throws Exception
	{
		Vector items = new Vector();
		
		KXmlParser parser = new KXmlParser();
		
		HttpConnection conn = (HttpConnection)Connector.open(rssUrl);
		
		InputStream rssStream = conn.openInputStream();
		
		InputStreamReader isr = new InputStreamReader(rssStream);
		
		parser.setInput(isr);
		
		parser.nextTag();
		
		parser.require(XmlPullParser.START_TAG, null, "FITHOU-EDU-Calendar");
		
		parser.nextTag();
		
		while(parser.getEventType() != XmlPullParser.END_TAG)
		{
			String nodeName = parser.getName();
			
			if(nodeName.compareTo("Calendar") == 0)
			{
				items.addElement(parseRssItem(parser));
			}
			else
			{
				parser.skipSubTree();
			}
			parser.nextTag();
		}
		
		isr.close();
		
		rssStream.close();
		
		conn.close();
		
		return items;
	}
        
        
	Lichhoc parseRssItem(KXmlParser parser) throws Exception
	{
		Lichhoc item = new Lichhoc();
		
		parser.nextTag();

		while(parser.getEventType() != XmlPullParser.END_TAG)
		{
			String nodeName = parser.getName();
			
			if(nodeName.compareTo("Ngayhoc") == 0)
			{
				item.setsNgayhoc(parser.nextText());
			}
			else if(nodeName.compareTo("Giobatdau") == 0)
			{
				item.setsGiobatdau(parser.nextText());
			}                       
                        else if(nodeName.compareTo("Gioketthuc") == 0)
			{
				item.setsGioketthuc(parser.nextText());
			}
                        else if(nodeName.compareTo("Trangthai") == 0)
			{
				item.setsTrangthai(parser.nextText());
			}
                        else if(nodeName.compareTo("GiangVien") == 0)
			{
				item.setsTengiangvien(parser.nextText());
			}
                        else if(nodeName.compareTo("Phonghoc") == 0)
			{
				item.setsTenphong(parser.nextText());
			}
                        else if(nodeName.compareTo("Monhoc") == 0)
			{
				item.setsTenmonhoc(parser.nextText());
			}
			else
			{
				parser.skipSubTree();
			}
			parser.nextTag();
		}
		return item;
	}
}
